package com.hck.domain.consultation.service;

import com.hck.domain.consultation.model.entity.ConsultationReplyEntity;
import com.hck.domain.consultation.model.entity.HealthConsultationEntity;
import com.hck.domain.consultation.repository.IConsultationReplyRepository;
import com.hck.domain.consultation.repository.IHealthConsultationRepository;
import com.hck.types.model.PageRequest;
import com.hck.types.model.PageResponse;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ConsultationDomainService {

    @Resource
    private IHealthConsultationRepository consultationRepository;
    @Resource
    private IConsultationReplyRepository replyRepository;

    // 创建咨询
    public Long createConsultation(HealthConsultationEntity consultation) {
        consultation.setStatus("pending");
        consultation.setCreateTime(new Date());
        consultationRepository.save(consultation);
        return consultation.getId();
    }

    // 回复咨询
    public void replyConsultation(Long consultationId, ConsultationReplyEntity reply) {
        reply.setConsultationId(consultationId);
        reply.setCreateTime(new Date());
        replyRepository.save(reply);

        // 更新主表状态
        HealthConsultationEntity consultation = consultationRepository.findById(consultationId);
        consultation.setStatus("replied");
        consultation.setReplyTime(new Date());
        consultationRepository.updateStatus(consultationId, "replied", new Date(), null);
    }

    // 查询咨询详情
    public HealthConsultationEntity getConsultationDetail(Long id) {
        return consultationRepository.findById(id);
    }

    // 查询咨询回复
    public List<ConsultationReplyEntity> getReplies(Long consultationId) {
        return replyRepository.findByConsultationId(consultationId);
    }

    // 查询用户/医生的咨询列表
    public List<HealthConsultationEntity> getUserConsultations(Long userId) {
        return consultationRepository.findByUserId(userId);
    }
    public List<HealthConsultationEntity> getDoctorConsultations(Long doctorId) {
        return consultationRepository.findByDoctorId(doctorId);
    }

    public void closeConsultation(Long consultationId) {
        consultationRepository.updateStatus(consultationId, "closed", null, new java.util.Date());
    }
    
    /**
     * 分页查询用户咨询列表
     */
    public PageResponse<HealthConsultationEntity> getUserConsultationsPage(Long userId, PageRequest pageRequest) {
        pageRequest.validate();
        
        // 获取总数
        Long total = consultationRepository.countByUserId(userId);
        
        // 获取分页数据
        List<HealthConsultationEntity> consultations = consultationRepository.findByUserIdPage(userId, pageRequest.getOffset(), pageRequest.getLimit());
        
        // 构建分页响应
        PageResponse<HealthConsultationEntity> response = PageResponse.<HealthConsultationEntity>builder()
                .data(consultations)
                .pageNum(pageRequest.getPageNum())
                .pageSize(pageRequest.getPageSize())
                .total(total)
                .build();
        
        response.calculatePageInfo();
        return response;
    }
    
    /**
     * 分页查询医生咨询列表
     */
    public PageResponse<HealthConsultationEntity> getDoctorConsultationsPage(Long doctorId, PageRequest pageRequest) {
        pageRequest.validate();
        
        // 获取总数
        Long total = consultationRepository.countByDoctorId(doctorId);
        
        // 获取分页数据
        List<HealthConsultationEntity> consultations = consultationRepository.findByDoctorIdPage(doctorId, pageRequest.getOffset(), pageRequest.getLimit());
        
        // 构建分页响应
        PageResponse<HealthConsultationEntity> response = PageResponse.<HealthConsultationEntity>builder()
                .data(consultations)
                .pageNum(pageRequest.getPageNum())
                .pageSize(pageRequest.getPageSize())
                .total(total)
                .build();
        
        response.calculatePageInfo();
        return response;
    }
}